<?php
/**
 * Created by PhpStorm.
 * User: xuewl
 * Date: 2018/1/3
 * Time: 17:21
 */

namespace app\goods\service;

use app\common\library\Service;
use think\Db;

class GoodsStock extends Service
{
    use \app\common\library\traits\Model;
    protected $model = null;

    protected function _initialize()
    {
        parent::_initialize();
        $this->model = model('goods/goods_stock');
    }


    public function create($params)
    {
        Db::startTrans();
        $result = $this->model->save($params);
        if(!$result) {
            $this->error = $this->model->getError();
            return false;
        }

        // 根据记录写入相关
        $tickets = explode("\r\n", $params['ticket']);

        $info = [];
        foreach ($tickets as $ticket) {
            $info[] = [
                'stock_id' => $this->model->id,
                'content' => $ticket
            ];
        }
        if($info) {
            $result = model('goods/goods_stock_ticket', 'service')->saveAll($info);
            if(!$result) {
                Db::rollback();
                return false;
            }
        }
        Db::commit();
        return $this->model;
    }


}
